﻿using System;
using System.ComponentModel.Composition;
using System.Text.RegularExpressions;
using Microsoft.Office.Interop.Word;

namespace Songhay.Office2010.WordWalkingStick.Commands
{
    using Songhay.Models;
    using Songhay.Office2010.Word;

    [Export(typeof(IOfficeWordCommand))]
    public class ReplaceWithGlyphsCommand : IOfficeWordCommand
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ReplaceWithGlyphsCommand"/> class.
        /// </summary>
        public ReplaceWithGlyphsCommand()
        {
            this.Name = "ReplaceWithGlyphsCommand";
        }

        #region IOfficeWordCommand Members

        /// <summary>
        /// Gets or sets the name
        /// (non-unique key)
        /// for locating this command
        /// in a collection of commands.
        /// </summary>
        /// <value>The name.</value>
        /// <remarks>
        /// Conventional command name prefixes:
        /// Startup* Commands with this prefix will be executed during the ThisAddIn_Startup event.
        /// Shutdown* Commands with this prefix will be executed during the ThisAddIn_Shutdown event.
        /// </remarks>
        public string Name { get; set; }

        /// <summary>
        /// Executes the command.
        /// </summary>
        /// <typeparam name="TMessage">The type of the message.</typeparam>
        /// <param name="wordApplication">The word application.</param>
        /// <param name="message">The message.</param>
        public void ExecuteCommand<TMessage>(Application wordApplication, TMessage message) where TMessage : IControlInteractionMessage
        {
            if(wordApplication.Selection.Characters.Count < 3)
                wordApplication.Selection.Expand(WdUnits.wdParagraph);

            var range = wordApplication.Selection.Range;
            range.Text = range.Text
                .Replace(" \v", "\v")
                .Replace("\v\v", "\v") //FUNKYKB: \v = line break
                .Replace(" \r", "\r")
                .Replace("\r\r", "\r")
                .Replace("\u00A0\r", "\r") //FUNKYKB: \u00A0 = non-breaking space; use Ctrl+Shift+Space in Word
                .Replace("  ", " ")
                .Replace("...", "…")
                .Replace(" '", " ‘")
                .Replace("' ", "’ ")
                .Replace(".'", ".’")
                .Replace(@" """, " “")
                .Replace(@""" ", "” ")
                .Replace(@".""", ".”")
                .Replace("---", "—")
                .Replace(" - ", "—")
                .Replace(" — ", "—");

            range.Text = Regex.Replace(range.Text, @"(\w)'(\w)", "$1’$2");
        }

        /// <summary>
        /// Executes the command for binding.
        /// </summary>
        /// <typeparam name="TMessage">The type of the message.</typeparam>
        /// <param name="wordApplication">The word application.</param>
        /// <param name="message">The message.</param>
        public object ExecuteCommandForBinding<TMessage>(Application wordApplication, TMessage message) where TMessage : IControlInteractionMessage
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
